<?php

require_once "Database.php";


class PostView {

	private $topic = 'topic';
	private $text = 'text';
	private $delete = 'delete';
	private $edit = 'edit';
	
	//konstanter som kan anropas i PostController
	const POSTVALFAIL = 0;
	const POSTSUCCESS = 1;
	const NOTLOGGEDIN = 2;
	
	//switch-sats för felhanteringsmeddelanden
	public static function statusMessage($n){
				$statusMessage = null;
				
				switch($n){
					case self:: POSTVALFAIL:
						$statusMessage .= "Posten godkändes inte i valideringen. Minst 6 tecken, högst 120.";
						break;
					case self:: POSTSUCCESS:
						$statusMessage .= "Inlägg postades.";
						break;
					case self:: NOTLOGGEDIN:
						$statusMessage .= "Du måste vara inloggad för att kunna posta inlägg!";
						break;
					}
		return "<p class='statusMessage'> $statusMessage</p>";
	}

	// Genom GET kan man anropa formuläret i andra funktioner
	public function DoPostBox() {

		$postForm = "
		<form id='form1' name='form1' method='post'>
		<td>
		<table width='100%' border='0' cellpadding='3' cellspacing='1' bgcolor='#FFFFFF'>
		<tr>
		<td colspan='3' bgcolor='#E6E6E6'><strong>Create New Topic</strong> </td>
		</tr>
		<tr>
		<td width='14%'><strong>Topic</strong></td>
		<td width='2%'>:</td>
		<td width='84%'><input name='" . $this -> topic . "' type='text' id='topic' size='50' /></td>
		</tr>
		<tr>
		<td valign='top'><strong>Text</strong></td>
		<td valign='top'>:</td>
		<td><textarea style='resize: none;' name='" . $this -> text . "' cols='50' rows='3' size='100' id='text'></textarea></td>
		</tr>
		<tr>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td><input type='submit' value='Post' /> <input type='reset' name='Submit2' value='Reset' /></td>
		</tr>
		</table>
		</td>
		</form>
		</tr>
		</table>
		";
		return $postForm;
	}
	
	public function ViewPosts($postArray){
		$ret = "";
		
		foreach($postArray->get() as $post) {
				$messageID = $post -> getpostID();
				
			$ret .= "
			<div id='posts'>
			<div id='post'><h3>Inlägg ".$messageID."</h3></div><div id='deleteBut'>".$this->DoDeletePostBut($messageID)."</div>
			<div id='post'><h4>".$post->gettopic()."</h4></div>
			<div id='post'><p>".$post->gettxt()."</p></div>
			</div>
			";
		}
		
		return $ret;
	}
	
	public function DoDeletePostBut($messageID) {
		$deleteButton = "<form method='POST'>
		<input type='hidden' value='".$messageID."' name='" . $this -> delete . "'/>
		<input type='submit' value='Delete'/></form>";
		return $deleteButton;
	}
	
	public function GetTopic() {
		if (isset($_POST[$this -> topic]) == true) {
			//return the variable
			return $_POST[$this -> topic];
		} 
		return NULL;
	}	
	
	public function GetMessageIDDelete() {
		if(isset($_POST[$this->delete])){
			return $_POST[$this->delete];
		}
		return false;
	}
	
	public function GetText() {
		if (isset($_POST[$this -> text]) == true) {
			//return the variable
			return $_POST[$this -> text];
		} 
		return NULL;
	}	
	public function TriedToPost() {
		if (($this -> GetTopic() != NULL) && ($this -> GetText() != NULL)) {
			return true;
		} else {
			return false;
		}
	}
	// anropar elementen för knappen och via isset kollar man om den trycks på
	public function TriedToDeletePost() {
		if (isset($_POST[$this -> delete]) == true){
			return true;
		} else {
			return false;
		}
	}
}
?>
